package com.jeesite.modules.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateTimeUtil {
    /**
     * 获取本周开始结束时间
     */
    public static List<String> thisWeek(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是不是周日，若是是则减一天计算周六的，不然会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 得到当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 得到当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String startTime = sdf.format(cal.getTime())+" 00:00:00";
        // System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String endTime = sdf.format(cal.getTime())+" 23:59:59";
        // System.out.println("所在周星期日的日期：" + imptimeEnd);
        List<String> time2 = new ArrayList<>();
        time2.add(startTime);
        time2.add(endTime);
        return time2;

    }

    /**
     * 获取上周开始结束时间
     */
    public static List<String> lastWeek(){
        Calendar calendar = Calendar.getInstance();

        calendar.setFirstDayOfWeek(Calendar.MONDAY);

        calendar.add(Calendar.DATE, -7);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        Date    sTime = calendar.getTime();

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        Date    eTime = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String startTime = sdf.format(sTime) + " 00:00:00";

        String endTime = sdf.format(eTime) + " 23:59:59";
        List<String> time2 = new ArrayList<>();
        time2.add(startTime);
        time2.add(endTime);
        return time2;
    }

    /**
     * 获取本月开始结束时间
     */
    public static List<String> thisMonth(){
        Calendar cal= Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        Date time = cal.getTime();
        String format = new SimpleDateFormat("yyyy-MM").format(time);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String startTime  =format+"-01 00:00:00";
        String  endTime  =format +"-"+ cal.get(Calendar.DAY_OF_MONTH)+" 23:59:59";
        List<String> time2 = new ArrayList<>();
        time2.add(startTime);
        time2.add(endTime);
        return time2;
    }

    /**
     * 获取本季度开始结束时间
     */
    public static List<String> thisJidu(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, ((int) cal.get(Calendar.MONTH) / 3) * 3);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int month = cal.get(Calendar.MONTH) + 1;
        int deviation = 0;
        int sum = month + deviation;
        int quotient = sum / 12;
        if (sum < 0) {
            int i = month + Math.abs(quotient) * 12 + deviation;
            cal.add(Calendar.YEAR, quotient);
            cal.set(Calendar.MONTH, i - 1);
        } else {
            int i = month - Math.abs(quotient) * 12 + deviation;
            cal.add(Calendar.YEAR, quotient);
            cal.set(Calendar.MONTH, i - 1);
        }
        String format = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        cal.add(Calendar.MONTH, 3);
        String startTime  =format+" 00:00:00";
        String  endTime  = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())+" 00:00:00";
        List<String> time = new ArrayList<>();
        time.add(startTime);
        time.add(endTime);
        return time;
    }


    public static List<String> thisYear(){
        List<String> time = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        String  startTime  ="\'"+(cal.get(Calendar.YEAR) )+"-01-01 00:00:00";
        String endTime  ="\'"+ (cal.get(Calendar.YEAR)  + 1)+"-01-01 00:00:00";
        time.add(startTime);
        time.add(endTime);
        return time;
    }

    public static List<String> timeString(String timeParam){
        if("1".equals(timeParam)){
            return  thisWeek();
        }else if("2".equals(timeParam)){
            return  lastWeek();
        }else if("3".equals(timeParam)){
            return thisMonth();
        }else if("4".equals(timeParam)){
            return thisJidu();
        }else if("5".equals(timeParam)){
            return thisYear();
        }
        return null;
    }
}
